package org.young.common.test;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.young.common.model.entity.User;

import java.util.Date;

/**
 * Created by young on 16/10/7.
 */
public class Send {

    private final static String QUEUE_NAME = "mq.queue";

    private final static String EXCHANGE_NAME = "logs";

//    private static final String TASK_QUEUE_NAME = "task_queue";

    private static final Logger LOGGER = LoggerFactory.getLogger(Send.class);


    public static void main(String[] argv) throws java.io.IOException {

//        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("localhost");
////        factory.setUsername("young");
////        factory.setPassword("young");
////        factory.setVirtualHost("/");
////        factory.setPort(5672);
//
//        Connection connection = factory.newConnection();
//        Channel channel = connection.createChannel();
//
//
//        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//        String message = "Hello World!";
//        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
//        System.out.println(" [x] Sent '" + message + "'");
//
//        channel.close();
//        connection.close();

        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("localhost");
        factory.setUsername("young");
        factory.setPassword("young");
        factory.setVirtualHost("/");
        factory.setPort(5672);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

        User user = new User();
        user.setPassword("123456");
        user.setSex(1);
        user.setCreateTime(new Date());
        user.setName("young");
        user.setStatus(1);
        user.setUpdateTime(new Date());
        user.setId(1L);

        String message = JSONObject.toJSONString(user);

        LOGGER.info(message);


        channel.basicPublish(EXCHANGE_NAME,"",null,message.getBytes());

//        String queueName = channel.queueDeclare().getQueue();

//        channel.queueBind(queueName,EXCHANGE_NAME,"");


        channel.close();
        connection.close();

    }

}
